package com.example.skillcinema.ui.screens.main

import android.os.Bundle
import android.view.FrameStats
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import com.example.skillcinema.R
import com.example.skillcinema.adapters.StaffRecyclerViewAdapter
import com.example.skillcinema.databinding.FragmentStaffListBinding
import com.example.skillcinema.ui.screens.main.viewmodels.StaffListViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
class StaffListFragment : Fragment() {
    private var staffId: Int? = null
    private var isActor: Boolean = false
    private var _binding: FragmentStaffListBinding? = null
    private val binding: FragmentStaffListBinding
        get() = _binding!!
    private val viewModel: StaffListViewModel by viewModels<StaffListViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            staffId = it.getInt(STAFF_ID_ARG)
            isActor = it.getBoolean(STAFF_IS_ACTOR, false)
            viewModel.loadIt(staffId!!, isActor)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        _binding = FragmentStaffListBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        (activity as? AppCompatActivity)?.setSupportActionBar(binding.toolbar)
        (activity as? AppCompatActivity)?.supportActionBar?.setDisplayHomeAsUpEnabled(true)
        (activity as? AppCompatActivity)?.supportActionBar?.setDisplayShowTitleEnabled(false)
        if (isActor)
            binding.toolbarTitle.text = getString(R.string.staff_toolbar_actors_title)
        else
            binding.toolbarTitle.text = getString(R.string.staff_toolbar_staff_title)
        binding.toolbar.setNavigationOnClickListener {
            findNavController().navigateUp()
        }
        val adapter: StaffRecyclerViewAdapter = StaffRecyclerViewAdapter({ id -> staffClicked(id) })
        binding.staffRecyclerView.adapter = adapter
        lifecycleScope.run {
            launch {
                viewModel.staffFlow.collect {
                    adapter.setData(it)
                }
            }
        }
    }

    private fun staffClicked(id: Int) {
        val bundle: Bundle = Bundle().apply {
            putInt(StaffDetailFragment.STAFF_ID_ARG, id)
        }
        findNavController().navigate(
            R.id.action_staffListFragment_to_staffDetailFragment,
            bundle
        )
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    companion object {
        @JvmStatic
        fun newInstance(param1: Int, param2: Boolean) = StaffListFragment().apply {
            arguments = Bundle().apply {
                getInt(STAFF_ID_ARG, param1)
                getBoolean(STAFF_IS_ACTOR, param2)
            }
        }

        const val STAFF_ID_ARG = "staff_id"
        const val STAFF_IS_ACTOR = "staff_is_actor"
    }
}